/********* CordovaPluginBaidulbs.m Cordova Plugin Implementation *******/

#import <Cordova/CDV.h>
#import <BMKLocationKit/BMKLocationComponent.h>

@interface CordovaPluginBaidulbs: CDVPlugin<BMKLocationAuthDelegate, BMKLocationManagerDelegate> {
    BMKLocationManager* locationManager;
}

- (void)getCurrentPosition:(CDVInvokedUrlCommand*)command;
@end

@implementation CordovaPluginBaidulbs

static int SUCCESS = 200;
static int PERMISSION_DENIED = BMKLocationErrorDenied;
static int POSITION_UNAVAILABLE = BMKLocationErrorLocFailed;
static int TIMEOUT = 3;
static int UNKNOWN_ERROR = BMKLocationErrorUnKnown;
static NSString* NAME_CODE = @"code";

- (void) onCheckPermissionState:(BMKLocationAuthErrorCode)iError {
    NSLog(@"iError错误信息：%ld", iError);
}

- (void)pluginInitialize {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"baidu" ofType:@"plist"];
    NSDictionary* baidu = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString* ak = [baidu valueForKey:@"API_KEY"];
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:ak authDelegate:self];
    
    locationManager = [[BMKLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.locationTimeout = 10;
    locationManager.reGeocodeTimeout = 10;
}

- (void)getCurrentPosition:(CDVInvokedUrlCommand*)command
{
    [locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        if(error) {
            long code = error.code < 3 ? error.code : UNKNOWN_ERROR;
            NSMutableDictionary* errorMessage = [self errorProperties:(int) code];
            CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary: errorMessage];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            NSLog(@"错误代码：%ld", error.code);
            return;
        }
        if(location) {
            NSMutableDictionary* message = [self errorProperties:SUCCESS];
            NSDictionary* coord = @{
                @"latitude": [[NSString alloc] initWithFormat:@"%g", location.location.coordinate.latitude],
                @"longitude": [[NSString alloc] initWithFormat:@"%g", location.location.coordinate.longitude]
            };
            [message setObject:coord forKey:@"coords"];
            CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: message];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }];
}
- (NSMutableDictionary*)errorProperties:(int) code
{
    NSMutableDictionary* result = [NSMutableDictionary dictionary];
    [result setValue:[NSNumber numberWithInt:code] forKey:NAME_CODE];
    [result setValue:[NSNumber numberWithInt:PERMISSION_DENIED] forKey:@"PERMISSION_DENIED"];
    [result setValue:[NSNumber numberWithInt:POSITION_UNAVAILABLE] forKey:@"POSITION_UNAVAILABLE"];
    [result setValue:[NSNumber numberWithInt:TIMEOUT] forKey:@"TIMEOUT"];
    [result setValue:[NSNumber numberWithInt:UNKNOWN_ERROR] forKey:@"UNKNOWN_ERROR"];
    return result;
}
@end
